package com.bdmc.smokesensor.services;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bdmc.smokesensor.dao.Smk_GroupCompanyJPA;
import com.bdmc.smokesensor.entity.Smk_GroupCompany;
import com.bdmc.smokesensor.iservices.ISmk_GroupCompanyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Service
public class Smk_GroupCompanyService extends BaseService<Smk_GroupCompany> implements ISmk_GroupCompanyService {

    @Autowired
    public Smk_GroupCompanyJPA _gcJPA;

    @Override
    public List<Smk_GroupCompany> findAll() {
        String sql = "select a.* from Smk_GroupCompany a";
        List<Smk_GroupCompany> list = GetListBySql(sql, Smk_GroupCompany.class);
        return list;
    }

    @Override
    public Map<String, Object> findByPageable(int depId, String treeRoot, String groupName, int page, int size) {
        Page<Map<String, Object>> p = _gcJPA.getCurrentGroupCompany(depId, treeRoot, groupName,
                PageRequest.of(page, size));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", p.getTotalElements());
        map.put("data", p.getContent());
        return map;
    }

    @Override
    public Smk_GroupCompany findOne(Example<Smk_GroupCompany> example) {
        return _gcJPA.findOne(example).orElse(null);
    }

    @Override
    public Smk_GroupCompany findOneById(int id) {
        return _gcJPA.findById(id).orElse(null);
    }

    @Override
    public Smk_GroupCompany save(Smk_GroupCompany d) {
        if (d.getId() < 1)
            d.setJoinDate(new Date());
        else
            d.setUpdateDate(new Date());
        return _gcJPA.save(d);
    }

    @Override
    public void deleteById(int id) {
        _gcJPA.deleteById(id);
    }

    @Override
    public void updateDelete(int id) {
        Smk_GroupCompany d = _gcJPA.findById(id).orElse(null);
        d.setDel(true);
        _gcJPA.save(d);
    }

    @Override
    public List<Map<String, Object>> findByDepId(int depid) {
        String sql = "select gc.id as value,gc.groupName as label from smk_groupcompany gc where gc.isDel=0 and gc.departmentId="
                + depid;
        return getListMapBySql(sql);
    }

    @Override
    public Map<String, Object> findAreaGCBDSum(int depid) {
        String sql = "SELECT (select count(*)  from sys_department d1" + " left JOIN sys_department dep on d1.id=dep.id"
                + " where (dep.id=" + depid + " or dep.treeRoot like '%," + depid + ",%') and d1.depType=2) areaCount,"
                + " (select count(*)  from smk_groupcompany gc"
                + " left JOIN sys_department dep on gc.departmentId=dep.id" + " where dep.id=" + depid
                + " or dep.treeRoot like '%," + depid + ",%') gcCount," + " (select count(*)  from smk_buildings bd "
                + " left JOIN sys_department dep on bd.departmentId=dep.id" + " where dep.id=" + depid
                + " or dep.treeRoot like '%," + depid + ",%') bdCount";
        return getListMapBySql(sql).get(0);
    }

    @Override
    public Map<String, Object> findAreaGcBdDevSum(int depid) {
        String sql = "SELECT (select count(*)  from sys_department d1" + " left JOIN sys_department dep on d1.id=dep.id"
                + " where (dep.id=" + depid + " or dep.treeRoot like '%," + depid + ",%') and d1.depType=2) areaCount,"
                + " (select count(*)  from smk_groupcompany gc"
                + " left JOIN sys_department dep on gc.departmentId=dep.id" + " where dep.id=" + depid
                + " or dep.treeRoot like '%," + depid + ",%') gcCount," + " (select count(*)  from smk_buildings bd "
                + " left JOIN sys_department dep on bd.departmentId=dep.id" + " where dep.id=" + depid
                + " or dep.treeRoot like '%," + depid + ",%') bdCount," + "(select count(*) from smk_devices dev"
                + " left JOIN sys_department dep on dev.departmentId=dep.id" + " where dep.id=" + depid
                + " or dep.treeRoot like '%," + depid + ",%') devCount";
        return getListMapBySql(sql).get(0);
    }

    @Override
    public Long countByDepartmentId(int dep_id) {
        return _gcJPA.countByDepartmentId(dep_id);
    }

}